package com.yx.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.iot.base.bean.Product;
import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductManager;
import com.yx.cloud.utils.RomUtils;

import java.util.List;

public class HelpSelectSeriesAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private Context mContext;
    private List<Product> mListData;
    private View view;
    private OnItemClickListener mOnItemClickListener = null;

    public HelpSelectSeriesAdapter(Context context, List<Product> list) {
        mContext = context;
        mListData = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.help_selectseries_list_item, parent, false);
        SelectSeriesViewHolder ssv = new SelectSeriesViewHolder(view);
        view.setOnClickListener(this);
        return ssv;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            ((SelectSeriesViewHolder) holder).mSeriesName.setText("APP");
            ((SelectSeriesViewHolder) holder).mSeriesIcon.setImageResource(R.mipmap.help_app_icon);
        } else {
            Product product = mListData.get(position);
            ((SelectSeriesViewHolder) holder).mSeriesName.setText(product.getProductName());
            ((SelectSeriesViewHolder) holder).mSeriesIcon.setImageResource(product.getProductIcon());
        }
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    /**
     * Item点击事件方法
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    class SelectSeriesViewHolder extends RecyclerView.ViewHolder {
        public TextView mSeriesName;
        public ImageView mSeriesIcon;

        public SelectSeriesViewHolder(View itemView) {
            super(itemView);
            mSeriesName = itemView.findViewById(R.id.tv_help_seriesName);
            mSeriesIcon = itemView.findViewById(R.id.iv_help_robotIcon);
        }
    }
}
